package com.fancon.android.gardenjokesx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.dialog.S03_6Dialog;
import com.fancon.android.gardenjokesx.entity.User;
import com.fancon.android.gardenjokesx.social.DatabaseHelper;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.UserUtil;

public class S03Activity extends GardenJokesBaseActivity {

	//private Button btnNext;
	private RadioButton mRadioFB;
	private RadioButton mRadioTwit;
	private RadioButton mRadioFlickr;
	private RadioButton mRadioTumblr;
	private RadioButton mRadioWeibo;
	
	private int screenID;
	private String userID;
	private String isSignIn;
	private ImageView s03_twitter_arrows, s03_facebook_arrows, s03_flickr_arrows, s03_tumblr_arrows, s03_weibo_arrows;
	
	private DatabaseHelper db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mesx_sharing_setting_s03);
		TextView title = (TextView) findViewById(R.id.action_bar_textview_title);
		title.setText(getText(R.string.S03_shareing_settings));
		
		View backBtn = findViewById(R.id.action_bar_button_back);
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		User user = new User();
		user = UserUtil.getInfoUserLogin(this);
		userID = user.getUser_id();

		if(getIntent().getExtras() != null){
			screenID = getIntent().getExtras().getInt(Constant.SCREEN_ID);
			isSignIn = getIntent().getExtras().getString(Constant.IS_SIGNIN);
		}

		db = new DatabaseHelper(S03Activity.this.getApplicationContext());
		//@binhbt-comment
		//btnNext = (Button) findViewById(R.id.s03_btnNext);
		//setOnClick(btnNext);
		
		if (screenID == Constant.S02_1 || Constant.IS_SIGNIN.equals(isSignIn)) {
			//btnNext.setVisibility(View.VISIBLE);
			isSignIn = Constant.IS_SIGNIN;
		} else {
			//btnNext.setVisibility(View.GONE);
		}

		s03_twitter_arrows = (ImageView) findViewById(R.id.s03_twitter_arrows);
		s03_facebook_arrows = (ImageView) findViewById(R.id.s03_facebook_arrows);
		s03_flickr_arrows = (ImageView) findViewById(R.id.s03_flickr_arrows);
		s03_tumblr_arrows = (ImageView) findViewById(R.id.s03_tumblr_arrows);
		s03_weibo_arrows = (ImageView) findViewById(R.id.s03_weibo_arrows);
		
//		TWITTER
		mRadioTwit = (RadioButton)findViewById(R.id.s03_radiobtnTwitter);		
		setOnClick(mRadioTwit);
		if (!db.isTwitterLinked(userID)) {
			s03_twitter_arrows.setVisibility(View.VISIBLE);
			mRadioTwit.setButtonDrawable(R.drawable.s03_radio_button_not_sellected);
		} else {
			s03_twitter_arrows.setVisibility(View.GONE);
			mRadioTwit.setButtonDrawable(R.drawable.s03_radio_button_sellected);
		}
		
//		FACEBOOK
		mRadioFB = (RadioButton)findViewById(R.id.s03_radiobtnFacebook);
		setOnClick(mRadioFB);
		if (!db.isFacebookLinked(userID)) {
			s03_facebook_arrows.setVisibility(View.VISIBLE);
			mRadioFB.setButtonDrawable(R.drawable.s03_radio_button_not_sellected);
		} else {
			s03_facebook_arrows.setVisibility(View.GONE);
			mRadioFB.setButtonDrawable(R.drawable.s03_radio_button_sellected);
		}
		
		
//		FLICKR
		mRadioFlickr = (RadioButton)findViewById(R.id.s03_radiobtnFlickr);
		setOnClick(mRadioFlickr);
		if (!db.isFlickrLinked(userID)) {
			s03_flickr_arrows.setVisibility(View.VISIBLE);
			mRadioFlickr.setButtonDrawable(R.drawable.s03_radio_button_not_sellected);
		} else {
			s03_flickr_arrows.setVisibility(View.GONE);
			mRadioFlickr.setButtonDrawable(R.drawable.s03_radio_button_sellected);
		}
		
		
//		TUMBLR
		mRadioTumblr = (RadioButton)findViewById(R.id.s03_radiobtnTumblr);
		setOnClick(mRadioTumblr);
		if (!db.isTumblrLinked(userID)) {
			s03_tumblr_arrows.setVisibility(View.VISIBLE);
			mRadioTumblr.setButtonDrawable(R.drawable.s03_radio_button_not_sellected);
		} else {
			s03_tumblr_arrows.setVisibility(View.GONE);
			mRadioTumblr.setButtonDrawable(R.drawable.s03_radio_button_sellected);
		}
		
		
//		WEIBO
		mRadioWeibo = (RadioButton)findViewById(R.id.s03_radiobtnWeibo);
		setOnClick(mRadioWeibo);
		if (!db.isWeiboLinked(userID)) {
			s03_weibo_arrows.setVisibility(View.VISIBLE);
			mRadioWeibo.setButtonDrawable(R.drawable.s03_radio_button_not_sellected);
		} else {
			s03_weibo_arrows.setVisibility(View.GONE);
			mRadioWeibo.setButtonDrawable(R.drawable.s03_radio_button_sellected);
		}
		
		db.close();
	}
	
	private void setOnClick(View v) {
		
		v.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				db = new DatabaseHelper(S03Activity.this.getApplicationContext());
				
				switch (v.getId()) {
				//@binhbt-comment
//				case R.id.s03_btnNext:
//					db.close();
//					startActivity(new Intent(getApplicationContext(), S04Activity.class));
//					break;
				
//				TWITTER
				case R.id.s03_radiobtnTwitter:
					
					if (db.isTwitterLinked(userID) == true) {
						S03_6Dialog dialogTwitter = new S03_6Dialog(S03Activity.this, mRadioTwit, db, s03_twitter_arrows);
						dialogTwitter.setMessage("Twitter");
						dialogTwitter.show();
						dialogTwitter = null;
						db.close();
					} else {
						db.close();
						Intent intent = new Intent(getApplicationContext(), S03_1Activity.class);
						intent.putExtra(Constant.SCREEN_ID, Constant.S03);
						intent.putExtra(Constant.USER_ID_VIEW, userID);
						intent.putExtra(Constant.IS_SIGNIN, isSignIn);
						startActivity(intent);
						finish();
					}
					
					break;
					
//				FACEBOOK	
				case R.id.s03_radiobtnFacebook:
					if (db.isFacebookLinked(userID) == true) {
						S03_6Dialog dialogFacebook = new S03_6Dialog(S03Activity.this, mRadioFB, db, s03_facebook_arrows);
						dialogFacebook.setMessage("Facebook");
						dialogFacebook.show();
						dialogFacebook = null;
						db.close();
					} else {
						db.close();
						Intent intent = new Intent(getApplicationContext(), S03_2Activity.class);
						intent.putExtra(Constant.SCREEN_ID, Constant.S03);
						intent.putExtra(Constant.IS_SIGNIN, isSignIn);
						startActivity(intent);
						finish();
					}
					
					break;
					
//				FLICKR	
				case R.id.s03_radiobtnFlickr:
					if (db.isFlickrLinked(userID) == true) {
						S03_6Dialog dialogFlickr = new S03_6Dialog(S03Activity.this, mRadioFlickr, db, s03_flickr_arrows);
						dialogFlickr.setMessage("Flickr");
						dialogFlickr.show();
						dialogFlickr = null;
						db.close();
					} else {
						db.close();
						Intent intent = new Intent(getApplicationContext(), S03_3Activity.class);
						intent.putExtra(Constant.SCREEN_ID, Constant.S03);
						intent.putExtra(Constant.IS_SIGNIN, isSignIn);
						startActivity(intent);
						finish();
					}
					
					break;
					
//				TUMBLR	
				case R.id.s03_radiobtnTumblr:
					if (db.isTumblrLinked(userID) == true) {
						S03_6Dialog dialogTumblr = new S03_6Dialog(S03Activity.this, mRadioTumblr, db, s03_tumblr_arrows);
						dialogTumblr.setMessage("Tumblr");
						dialogTumblr.show();
						dialogTumblr = null;
						db.close();
					} else {
						db.close();
						Intent intent = new Intent(getApplicationContext(), S03_4Activity.class);
						intent.putExtra(Constant.SCREEN_ID, Constant.S03);
						intent.putExtra(Constant.IS_SIGNIN, isSignIn);
						startActivity(intent);
						finish();
					}
					
					break;
					
//				WEIBO	
				case R.id.s03_radiobtnWeibo:
					
					if (db.isWeiboLinked(userID) == true) {
						S03_6Dialog dialogWeibo = new S03_6Dialog(S03Activity.this, mRadioWeibo, db, s03_weibo_arrows);
						dialogWeibo.setMessage("Weibo");
						dialogWeibo.show();
						dialogWeibo = null;
						db.close();
					} else {
						db.close();
						Intent intent = new Intent(getApplicationContext(), S03_7Activity.class);
						intent.putExtra(Constant.SCREEN_ID, Constant.S03);
						intent.putExtra(Constant.IS_SIGNIN, isSignIn);
						startActivity(intent);
						finish();
					}
					
					break;
					
					
				default:
					break;
				}
			} // end Onclick
		});	// End OnclickListener
	}	// End setOnclick


}
